﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartKeeper
{
    class EntityCache
    {
        private readonly Dictionary<Type, Dictionary<string, object>> _caches = new Dictionary<Type, Dictionary<string, object>>();

        public void Set(Type type, string id, object entity)
        {
            Dictionary<string, object> dict;
            if (!_caches.TryGetValue(type, out dict))
            {
                dict = new Dictionary<string, object>();
                _caches[type] = dict;
            }
            dict[id] = entity;
        }

        public T Get<T>(Type type, string id) where T : class 
        {
            
            Dictionary<string, object> dict;
            if (_caches.TryGetValue(type, out dict))
            {
                object obj;
                if (dict.TryGetValue(id, out obj))
                {
                    return obj as T;
                }
                return null;
            }
            return null;
        }
    }
}
